// lab1.cpp - see lab1.h
#include "lab1.h"

// see lab1.h
bool isLeapYear(int year) {
    bool isLeapYear = false;
    if (year % 4 == 0 || year == 2000) {
        isLeapYear = true;
    }
    return isLeapYear;
}

// see lab1.h
int validateMonth(string input) {
    string sMonth = input.substr(0,2);
    int month = atoi(sMonth.c_str());
    if (month < 0 || month > 12) {
        cout << "Invalid Month of (" << sMonth;
        cout << ") entered - value must be between 1 and 12.\n";
        month = -1;
    } 
    return month;
}

// see lab1.h
int validateYear(string input) {
    string sYear = input.substr(6,2);
    int year = atoi(sYear.c_str());
    // If the year is between 50 and 99, consider it as a date
    //    in the 1900s.  Otherwise, treat it as from the 2000s.
    if (year >= 50) { year += 1900; }
    else {year += 2000; }
    return year;   
}

// see lab1.h
int validateDate(string input, int year, int month) {
    bool isValid = false;
    string sDate = input.substr(3,2);
    int date = atoi(sDate.c_str());
    
    char errMsg[255];
    if (date < 0) {
        sprintf(errMsg, "Date %i is less than 0", date);
    } else {
        int maxDate = -1;
        switch (month) {
            // 30 days in Apr, Jun, Sep, Nov
            case 4:
            case 6:
            case 9:
            case 11:
                maxDate = 30;
                break;
            // Feb has 28 normally, 29 in a leap year
            case 2:
                if (!isLeapYear(year)) { maxDate = 28; }
                else { maxDate = 29; }
                break;
            // 31 days in Jan, Mar, May, Jul, Aug, Oct, Dec
            default:
                maxDate = 31;
                break;
        }
        
        // Compare date to the max determined above
        if (date > maxDate) {
            sprintf(errMsg, "Date in year %i and month %i must be between 1 and %i, but input was %i.", year, month, maxDate, date);            
        } else {
            isValid = true;
        }
    }

    if (!isValid) {
        cout << "Invalid Date - " << string(errMsg) << "\n";
        date = -1;
    }
    
    return date;
}

// see lab1.h
bool convertDate(string inputDate) {
    bool isValid = false;
    
    // Date must be in the m/dd/yy format, or we kick it out
    if (inputDate.length() != 8 || 
        inputDate.at(2) != '/' || 
        inputDate.at(5) != '/') {
        cout << "Invalid input of (" << inputDate;
        cout << "), dates must be in format (mm/dd/yy).";
        cout << "\nPress any key to continue.";
        string throwaway; 
        getline(cin, throwaway);
    } else {
        // Date string should be correct, so attempt conversion
        int year = validateYear(inputDate);
        int month = validateMonth(inputDate);
        int date = validateDate(inputDate, year, month);
        
        if (year != -1 && month != -1 && date != -1) {
            // Values are good, so munge.
            char tmp[255];
            sprintf(tmp, "%i/%i/%i", month, date, year);
            cout << "\nConverted date " << inputDate << " to format " << tmp;
            cout << "\nPlease press any key to continue.";
            string throwaway;
            getline(cin, throwaway);
            isValid = true;
        }
    }
    
    return isValid;
}

// see lab1.h
int main() {
    bool isFinished = false;
    while (!isFinished) {
        // Loop while we don't get the Q command
        cout << "\t(I) Enter Date.\n\t(Q) Quit Program.\n";
        cout << "Please press I to input a date, or Q to exit. ";
        cout << "Your choice: ";

        string choice;
        getline(cin, choice);        
        char selected = -1;
        if (choice.length() > 0) { selected = choice.at(0); }

        string input;
        switch (selected) {
            case 'Q':
            case 'q':
                isFinished = true;
                break;
            case 'I':
            case 'i':
                cout << "Please input a date using mm/dd/yy format: ";
                getline(cin, input);
                convertDate(input);
                continue;
            default:
                cout << "Invalid input.  Please choose I or Q.\n";
                continue;
        }

    }
}